import java.util.Stack;

public class Sort {
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    //希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
    }

    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }
    public static void swap(int[] array,int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length-1; j++) {
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    public static void selectSort2(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left < right){
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right ; i++) {
                if (array[i] < array[minIndex]){
                    minIndex = i;
                }
                if(array[i] > array[maxIndex]){
                    maxIndex = i;
                }
            }
            swap(array,minIndex,left);
            if(maxIndex == left){
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }

    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if (!flg){
                break;
            }
        }
    }

    //快速排序
    public static void quickSort(int[] array){
//        quick1(array,0,array.length-1);
        quick(array,0,array.length);
    }
    private static void quick1(int[] array, int start, int end) {
        if(start >= end){
            return;
        }
        int pivot = partitionHoare(array,start,end);
        quick1(array,start,pivot-1);
        quick1(array,pivot+1,end);
    }
    private static int partitionHoare(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while(left < right){
            while(left<right && array[right] >= array[left]){
                right--;
            }
            while (left<right && array[right] >= array[left]){
                left++;
            }
            swap(array, i, left);
        }
        return left;
    }

    //挖坑法
    public static int partitionHole(int[] array, int left, int right){
        int tmp = array[left];
        while(left < right){
            while(left<right && array[right] >= array[left]){
                right--;
            }
            array[left] = array[right];
            while (left<right && array[right] >= array[left]){
                left++;
            }
            array[right] = array[left];
            array[left] = tmp;
        }
        return left;
    }

    //前后指针法
    private static int partition(int[] array, int left, int right) {
        int prev = left;
        int cur = left+1;
        while(cur <= right){
            if(array[cur] < array[left] && array[++prev] != array[cur]){
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,prev,cur);
        return prev;
    }

    //找到中位数的下标
    public static int middleNum(int[] array, int left, int right){
        int mid = (left+right)/2;
        if(array[left] < array[right]){
            if(array[mid] < array[left]){
                return left;
            }else if(array[mid] > array[right]){
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[mid] < array[right]){
                return right;
            }else if(array[mid] > array[left]){
                return left;
            }else {
                return mid;
            }
        }
    }
    public static void quick(int[] array, int start, int end) {
        if(start >= end){
            return;
        }
//        System.out.println("start:"+start+"end:"+end);
        if(end - start + 1 <= 15){
            insertSort(array,start,end);
            return;
        }
        int index = middleNum(array,start,end);
        swap(array,index,start);
        int pivot = partitionHoare(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    public static void insertSort(int[] array,int left,int right) {
        for (int i = left+1; i < right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    //非递归快速排序
    public static void quickSortNor(int[] array){
        int start = 0;
        int end = array.length-1;
        Stack<Integer> stack = new Stack<>();
        int pivot = partitionHoare(array,start,end);
        if(pivot > start+1){
            stack.push(start);
            stack.push(pivot-1);
        }
        if(pivot+1 < end){
            stack.push(pivot+1);
            stack.push(end);
        }
        while(!stack.empty()){
            end = stack.pop();
            start = stack.pop();
            pivot = partitionHoare(array,start,end);
            if(pivot > start+1){
                stack.push(start);
                stack.push(pivot-1);
            }
            if(pivot+1 < end){
                stack.push(pivot+1);
                stack.push(end);
            }
        }
    }

    //归并排序
    public static void mergeSort(int[] array){
        mergeSortFunc(array,0,array.length-1);
    }
    public static void mergeSortFunc(int[] array, int start, int end){
        if(start >= end){
            return;
        }
        int mid = (start+end)/2;
        mergeSortFunc(array,start,mid);
        mergeSortFunc(array,mid+1,end);
        merge(array,start,mid,end);
    }

    private static void merge(int[] array, int left, int mid, int right) {
        int s1 = left;//可以不定义，为了好理解
        int e1 = mid;//可以不定义，为了好理解
        int s2 = mid+1;
        int e2 = right;//可以不定义，为了好理解
        //定义一个新的数组
        int[] tmpArr = new int[right-left+1];
        int k = 0;//tmpArr的下标
        //同时满足，证明两个归并段都有数据
        while(s1<=e1 && s2<=e2){
            if(array[s1] <= array[s2]){
                tmpArr[k++] = array[s1++];
            }else {
                tmpArr[k++] = array[s2++];
            }
        }
        while(s1 <= e1){
            tmpArr[k++] = array[s1++];
        }
        while(s2 <= e2){
            tmpArr[k++] = array[s2++];
        }
        //把排好序的数据拷贝回原来的数组当中
        for (int i = 0; i < tmpArr.length; i++) {
            array[i+left] = tmpArr[i];
        }
    }
}
